package com.space.school.controller;

import com.space.school.dao.OITDao;
import com.space.school.dao.impl.OITDaoImpl;
import com.space.school.domain.Info;
import com.space.school.domain.Master;
import com.space.school.service.InfoService;
import com.space.school.service.OITService;
import com.space.school.service.impl.InfoServiceImpl;
import com.space.school.service.impl.OITServiceImpl;
import com.space.school.utils.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "OITOperateServlet",value = "/OITOperateServlet")
public class OITOperateServlet extends HttpServlet {

    private OITService oitService = new OITServiceImpl();

    private InfoService infoService = new InfoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String order_id = req.getParameter("order_id");
        String operate = req.getParameter("operate");
//对应操作
        if(operate.equals("del")){
//删除操作=
        oitService.delete(order_id);
        resp.sendRedirect("/O_I_T");
        }else if(operate.equals("update")){
//        修改操作
        List<Master> master=oitService.findByOrder_id(order_id);
        if(master!=null){
            req.setAttribute("master",master.get(0));
            req.getRequestDispatcher("masterUpdate.jsp").forward(req, resp);
        }
        }else if(operate.equals("details")){
//        详情操作
            List<Info> info=infoService.findByUser(order_id);
            req.setAttribute("info",info);
            req.setAttribute("order_id",order_id);
            req.getRequestDispatcher("order_details_information_list.jsp").forward(req, resp);
        }else if(operate.equals("status")){
            oitService.finished(order_id);
            resp.sendRedirect("/O_I_T");
        }else if(operate.equals("pay")){
            oitService.finished_pay(order_id);
            resp.sendRedirect("/O_I_T");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        修改的表单提交操作
        req.setCharacterEncoding("UTF-8");

        String buyer_name = req.getParameter("buyer_name");
        String buyer_phone = req.getParameter("buyer_phone");
        String buyer_address = req.getParameter("buyer_address");
        String buyer_openid = req.getParameter("buyer_openid");
        String order_amount = req.getParameter("order_amount");
        String order_total = req.getParameter("order_total");
        String order_status = req.getParameter("order_status");
        String pay_status = req.getParameter("pay_status");
        String order_id = req.getParameter("order_id");
        Master master=new Master();
        master.setBuyer_name(buyer_name);
        master.setBuyer_phone(buyer_phone);
        master.setBuyer_address(buyer_address);
        master.setBuyer_openid(buyer_openid);
        master.setOrder_amount(Double.valueOf(order_amount));
        master.setOrder_total(order_total);
        master.setOrder_id(order_id);
        master.setOrder_status(Integer.parseInt(order_status));
        master.setPay_status(Integer.parseInt(pay_status));



        String action = req.getParameter("action");
        if(action.equals("next")){
            oitService.update(master);
            resp.sendRedirect("/O_I_T");
        }
    }
}
